package com.blindBox.back.utils;

import com.blindBox.back.commonResult.MsgBackToFront;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//JSON工具类
public class JsonUtils
{
    private static final ObjectMapper objetcMapper=new ObjectMapper();
    private static final HttpServletResponse response=SpringContextUtils.getHttpServletResponse();
    //处理返回给前端的数据
    public static void handleJsonDataBackToFront(Integer code,String status,Object data) {
        try {
            MsgBackToFront msgBackToFront = new MsgBackToFront(data, status);
            response.setStatus(code);
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().write(objetcMapper.writeValueAsString(msgBackToFront));
            response.getWriter().flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
